package org.example.weibo.controller;

import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import org.example.weibo.base.response.JsonResult;
import org.example.weibo.base.response.StatusCode;
import org.example.weibo.mapper.WeiboMapper;
import org.example.weibo.pojo.dto.WeiboAddParam;
import org.example.weibo.pojo.entity.Weibo;
import org.example.weibo.pojo.vo.UserVO;
import org.example.weibo.pojo.vo.WeiboDetailVO;
import org.example.weibo.pojo.vo.WeiboIndexVO;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("v1/weibo")
@Tag(name = "微博")
@Validated
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;
    @PostMapping("/insert")
    public JsonResult insert(@RequestBody WeiboAddParam weiboAddParam, HttpSession session){
        UserVO userVO = (UserVO) session.getAttribute("user");
//        if (userVO == null) {
//            return new JsonResult(StatusCode.NOT_LOGIN);
//        }
        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboAddParam,weibo);
        weibo.setCreateTime(new Date());
        weibo.setUserId(userVO.getId());
        int num = weiboMapper.insert(weibo);
        return JsonResult.success();
    }
    @GetMapping("/list")
    public JsonResult selectList(){
       List<WeiboIndexVO> list =  weiboMapper.select();
        return JsonResult.success(list);
    }
    @GetMapping("/select")
    public JsonResult selectDetail(@Range(min=100,max = 500,message = "编号id必须在100-500之间") Long id){
        WeiboDetailVO weiboDetailVO = weiboMapper.selectDetail(id);
        return JsonResult.success(weiboDetailVO);
    }
}
